package com.ncr.travel.appmgr.client.view;

import java.util.List;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;
import com.ncr.travel.appmgr.client.ConfigResources;
import com.ncr.travel.appmgr.client.ConfigurationItem;
import com.ncr.travel.appmgr.client.model.ScreenConfigurationFactory;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.presenter.GraphicsEditorPresenter;
import com.ncr.travel.appmgr.client.ui.GraphicsDiscPanel;
import com.ncr.travel.appmgr.model.shared.Graphics;
import com.ncr.travel.appmgr.model.shared.ScreenConfiguration;

/**
 * @author nd185038
 * 
 */
public class GraphicsEditorWidget extends Composite implements GraphicsEditorView {
	
    /**
     * Factory for screen config model objects.
     */
	private ScreenConfigurationFactory screenConfigurationFactory;

	private final ScrollPanel graphicsUpdateScrollPanel;
	private final FlexTable flexTable;
	private final VerticalPanel graphicsPanel;
	private final ConfigEditorView configEditorWidget;
	private final HorizontalPanel updatePanel = new HorizontalPanel();
	private final EventBus eventBus;
	private long screenId;
	private GraphicsEditorPresenter presenter;
	private ScreenConfiguration screenConfiguration;

	/**
	 * Constructor
	 * 
	 * @param eventBus
	 * @param graphicsProvider
	 * @param screenId
	 */
	@Inject
	public GraphicsEditorWidget(EventBus eventBus, ScreenConfigurationFactory screenConfigurationFactory) {
		this.eventBus = eventBus;
		this.screenConfigurationFactory = screenConfigurationFactory;
		System.out.println("Constructing GraphicsEditorWidget");

		graphicsPanel = new VerticalPanel();

		//ND01022012, Commented, as size details come from CSS style def.
		//graphicsPanel.setSize("100%", "100%");
		graphicsPanel.setStyleName("graphicsPanel");

		flexTable = new FlexTable();

		graphicsUpdateScrollPanel = new ScrollPanel();

		graphicsUpdateScrollPanel.setStyleName("graphicsUpdateScrollPanel");

		//ND01022012, Commented, as size details come from CSS style def
		//graphicsUpdateScrollPanel.setSize("764px", "350px");




		configEditorWidget = new ConfigEditorWidget("Are you sure you want to confirm graphics configuration changes?", eventBus);
		configEditorWidget.setConfigurationItem(ConfigurationItem.GRAPHICS);
		configEditorWidget.setScreenConfiguration(screenConfiguration);

		configEditorWidget.setSize("100%", "100%");

		// int colNum = 0;

		// final ImageResources resources = GWT.create(ImageResources.class);

		VerticalPanel functionalPanel = new VerticalPanel();
		
		//ND01022012, Commented, as size details come from CSS style def.
		//functionalPanel.setSize("750px", "95px");
		functionalPanel.setStyleName("functionalPanel");

		HTML splitter = new HTML("<hr>");
		splitter.setStyleName("splitter");

		functionalPanel.add(splitter);

		HorizontalPanel functionButtonsPanel = new HorizontalPanel();

		//ND01022012, Commented, as size details come from CSS style def.
		//functionButtonsPanel.setSize("100%", "100%");
		functionButtonsPanel.setStyleName("functionButtonsPanel");

		functionButtonsPanel.add(configEditorWidget.asWidget());

		functionalPanel.add(functionButtonsPanel);

		updatePanel.setVisible(false);
		graphicsUpdateScrollPanel.add(flexTable);

		graphicsPanel.add(graphicsUpdateScrollPanel);
		graphicsPanel.add(functionalPanel);
	    initWidget(graphicsPanel);
	}

	public void setScreenConfiguration(final ScreenConfiguration screenConfiguration) {
		this.screenConfiguration = screenConfiguration;
	}

	public void setScreenId(final long id) {
		this.screenId = id;
	}

	public void setGraphics(List<Graphics> graphicsList) {
		int rowIndex = 0;
		int colIndex = 0;
		for (Graphics graphics : graphicsList) {

			final GraphicsDiscPanel newPanel = new GraphicsDiscPanel(screenConfigurationFactory, ConfigResources.INSTANCE.initialConfiguration().getText(), this.screenId, graphics, configEditorWidget);// undoButton,
			// previewButton,
			// confirmButton);
			flexTable.setWidget(rowIndex, colIndex, newPanel);

			newPanel.getGraphicItemDiscPanel().addOpenHandler(new OpenHandler<DisclosurePanel>() {

				@Override
				public void onOpen(OpenEvent<DisclosurePanel> event) {

					updatePanel.setVisible(true);

					updatePanel.add(newPanel);
					updatePanel.setStyleName("updatePanel");

					flexTable.setVisible(false);

					graphicsUpdateScrollPanel.remove(flexTable);

					graphicsUpdateScrollPanel.add(updatePanel);

				}

			});
			newPanel.getGraphicItemDiscPanel().addCloseHandler(new CloseHandler<DisclosurePanel>() {

				@Override
				public void onClose(CloseEvent<DisclosurePanel> event) {
					int rIdx = 0;
					int cIdx = 0;
					boolean isEmptyCell = false;
					for (rIdx = 0; rIdx < flexTable.getRowCount(); rIdx++) {
						for (cIdx = 0; cIdx < 2; cIdx++) {
							if (flexTable.getWidget(rIdx, cIdx) == null) {
								// Window.alert("Row: " + rIdx + " Col: " + cIdx
								// + " is null");
								isEmptyCell = true;
								break;
							}
						}
						if (isEmptyCell == true)
							break;
					}
					// Window.alert("Setting Back flexTable's cell at Row: " +
					// rIdx + " Col: " + cIdx + " is null");
					flexTable.setWidget(rIdx, cIdx, newPanel);

					flexTable.setVisible(true);

					updatePanel.setVisible(false);

					graphicsUpdateScrollPanel.remove(updatePanel);

					graphicsUpdateScrollPanel.add(flexTable);
				}

			});
			colIndex++;
			if (colIndex > 1) {
				// reset for a new row.
				rowIndex++;
				colIndex = 0;
			}
		}
	}

	/**
	 * @return the flexTable
	 */
	@Override
	public FlexTable getFlexTable() {
		return flexTable;
	}

	@Override
	public void setPresenter(GraphicsEditorPresenter presenter) {
		this.presenter = presenter;
	}
}
